/*
 * Copyright (c) 2017,安徽皖通科技股份有限公司 All rights reserved.
 */

package com.wttech.dzzf.process.dao;

import com.wtkj.dao.impl.ListResultSetExtractor;
import com.wtkj.vo.dzzf.base.kfwd.*;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * 数据迁移Dao
 *
 * @author MaShibo
 * @date 2017/11/6.
 */
@Repository
public class DataTransferDao {

    @Resource(name = "jdbcTemplate")
    private JdbcTemplate testJdbcTemplate;

    @Resource(name = "dzzfJdbcTemplate")
    private JdbcTemplate dzzfJdbcTemplate;

    private static final String QUERY_SIIMPLE_COUNT_SQL = "SELECT COUNT(1) FROM %s ";

    //~ 资金账户
    // =========================================================================


    /**
     * 查询待迁移的资金张户信息数量
     *
     * @return 资金账户数量
     */
    public Integer queryFundAccountCount() {
        return testJdbcTemplate.queryForObject(String.format(QUERY_SIIMPLE_COUNT_SQL, "AC_FUNDACCOUNT"), Integer.class);
    }

    /**
     * 根据行号查询资金账户信息
     *
     * @param startRow 起始行号
     * @param endRow   终止行号
     * @return 资金账户信息
     */
    public List<FundAccount> queryFundAccountInfs(int startRow, int endRow) {
        final String sql =
                "SELECT * " +
                        "  FROM (SELECT a.*, rownum AS rm FROM ac_fundaccount a) " +
                        " WHERE rm >= ? " +
                        "   AND rm <= ? ";
        return (List<FundAccount>) testJdbcTemplate.query(sql, new ListResultSetExtractor(new FundAccount()), startRow, endRow);
    }

    /**
     * 根据资金账户ID查询是否存在
     *
     * @param fundAccountId 资金账户ID
     * @return 资金账户数量
     */
    public Integer queryFundAccountId(String fundAccountId) {
        return dzzfJdbcTemplate.queryForObject("SELECT COUNT(1) FROM AC_FUNDACCOUNT A WHERE A.FUNDACCOUNTID=? ", Integer.class, fundAccountId);
    }

    /**
     * 查询资金账户编号是否存在
     *
     * @param fundAccountNo 资金账户编号
     * @return 资金账户数量
     */
    public Integer queryFundAccountNo(String fundAccountNo) {
        return dzzfJdbcTemplate.queryForObject("SELECT COUNT(1) FROM AC_FUNDACCOUNT A WHERE A.FUNDACCOUNTNO=? ", Integer.class, fundAccountNo);
    }

    /**
     * 根据资金账户ID查询业务账户信息
     *
     * @param fundAccountId 资金账户ID
     * @return 业务账户信息
     */
    public List<Account> queryAccountInfosByFundAccountId(String fundAccountId) {
        return (List<Account>) testJdbcTemplate.query("SELECT * FROM AC_ACCOUNT A WHERE A.FUNDACCOUNTID = ? ", new ListResultSetExtractor(new Account()), fundAccountId);
    }

    //~ 业务账户
    // ======================================================================

    /**
     * 根据业务账户ID查询业务账户是否存在
     *
     * @param accountId 业务账户ID
     * @return 业务账户数量
     */
    public Integer queryAccountId(String accountId) {
        return dzzfJdbcTemplate.queryForObject("SELECT COUNT(1) FROM AC_ACCOUNT A WHERE A.ACCOUNTiD=? ", Integer.class, accountId);
    }

    /**
     * 根据业务账户编号查询业务账号是否存在
     *
     * @param accountNo 业务账户编号
     * @return 业务账户数量
     */
    public Integer queryAccountNo(String accountNo) {
        return dzzfJdbcTemplate.queryForObject("SELECT COUNT(1) FROM AC_ACCOUNT A WHERE A.ACCOUNTNO=? ", Integer.class, accountNo);
    }

    //~ 开户操作记录
    // =============================================================================

    /**
     * 根据业务账户ID查询开户操作记录
     *
     * @param accountId 业务账户ID
     * @return 开户操作记录
     */
    @SuppressWarnings("uncheck")
    public Optional<AccountOpt> queryAccountOpt(String accountId) {
        List<AccountOpt> accountOpts = (List<AccountOpt>) testJdbcTemplate.query("SELECT  * FROM AC_ACCOUNTOPT A WHERE A.ACCOUNTID=?" +
                " AND A.STATE=1 AND A.OPTTYPE = 1010 ", new ListResultSetExtractor(new AccountOpt()), accountId);
        return CollectionUtils.isEmpty(accountOpts) ? Optional.empty() : Optional.of(accountOpts.get(0));
    }

    /**
     * 根据操作记录ID查询开户操作记录是否存在
     *
     * @param optId 开户操作ID
     * @return 开户操作记录
     */
    public Integer queryOptId(String optId) {
        return dzzfJdbcTemplate.queryForObject("SELECT COUNT(1) FROM AC_ACCOUNTOPT A WHERE A.OPTID = ? ", Integer.class, optId);
    }

    /**
     * 根据交易流水号查询开户操作记录是否存爱
     *
     * @param tradeNo 开户交易流水号
     * @return 看球操作记录
     */
    public Integer queryTradeNo(String tradeNo) {
        return dzzfJdbcTemplate.queryForObject("SELECT COUNT(1) FROM AC_ACCOUNTOPT A WHERE A.TRADENO = ? ", Integer.class, tradeNo);
    }

    // ~卡片信息
    // ===================================================================================================

    /**
     * 根据业务账户ID查询卡片信息
     *
     * @param accountId 业务账户ID
     * @return 卡片信息
     */
    @SuppressWarnings("uncheck")
    public List<CardInfo> queryCardInfosByAccountId(String accountId) {
        return (List<CardInfo>) testJdbcTemplate.query("SELECT * FROM AC_CARDINFO A WHERE A.ACCOUNTID = ? ", new ListResultSetExtractor(new CardInfo()), accountId);
    }


    /**
     * 根据卡片ID查询卡片信息是否存在
     *
     * @param cardInfoId 卡片ID
     * @return 卡片信息数量
     */
    public Integer queryCardInfo(String cardInfoId) {
        return dzzfJdbcTemplate.queryForObject("SELECT COUNT(1) FROM AC_CARDINFO A WHERE A.CARDINFOID = ? ", Integer.class, cardInfoId);
    }

    /**
     * 根据车辆信息查询卡片信息是否存在
     *
     * @param vehPlate 车牌号
     * @param vehColor 车牌颜色
     * @return 卡片信息数量
     */
    public Integer queryCardInfoByVehInfos(String vehPlate, Integer vehColor) {
        return dzzfJdbcTemplate.queryForObject("SELECT COUNT(1) FROM AC_CARDINFO A WHERE A.VEHICLEPLATE=? AND A.VEHICLECOLOR=? AND A.ECARDSTATUS=1", Integer.class, vehPlate, vehColor);
    }

    /**
     * 根据卡片信息中的车辆信息查询车辆信息记录
     *
     * @param vehPlate 车牌号
     * @param vehColor 车牌颜色
     * @return 车辆信息记录
     */
    @SuppressWarnings("uncheck")
    public VehicleInfo queryVehInfoByVehInfos(String vehPlate, Integer vehColor) {
        List<VehicleInfo> vehicleInfos = (List<VehicleInfo>) testJdbcTemplate.query("SELECT * FROM AC_VEHICLEINFO A WHERE A.VEHPLATE=? AND A.VEHCOLOR=? ", new ListResultSetExtractor(new VehicleInfo()), vehPlate, vehColor);
        return CollectionUtils.isEmpty(vehicleInfos) ? null : vehicleInfos.get(0);
    }

    // ~ 车辆信息
    // =================================================================================

    @SuppressWarnings("uncheck")
    public VehicleInfo queryDzzfVehicleInfoByVehInfos(String vehPlate, Integer vehColor) {
        List<VehicleInfo> vehicleInfos = (List<VehicleInfo>) dzzfJdbcTemplate.query("SELECT * FROM AC_VEHICLEINFO A WHERE A.VEHPLATE=? AND A.VEHCOLOR=? ", new ListResultSetExtractor(new VehicleInfo()), vehPlate, vehColor);
        return CollectionUtils.isEmpty(vehicleInfos) ? null : vehicleInfos.get(0);
    }

}
